import React, {PureComponent} from "react";
import {Icon, notification, Popconfirm, Table} from "antd";
import {connect} from "react-redux";
import {tableProps} from "../../lib/ui";
import {del, query} from "./actions";
import {containerOrderList} from "../home/actions";

class List extends PureComponent {

    handleDelete = id => {
        const {dispatch, filter} = this.props;
        dispatch(del(id)).then(action => {
            if (action.error !== true) {
                notification.success({
                    message: '删除成功'
                });
                dispatch(query({...filter}));
            } else {
                notification.error({
                    message: '删除失败:' + action.message
                });
            }
        });
        ;
    };

    componentWillMount() {
        const {dispatch} = this.props;
        dispatch(query({}));
    };

    render() {
        const columns = [
            {
                title: "业务类型",
                dataIndex: "name",
                width: "150px",
            }, {
                title: "单号规则前缀",
                dataIndex: "prefix",
                width: "150px",
            }, {
                title: '功能',
                dataIndex: '',
                key: 'x',
                width: "80px",
                render: (text, record) => <span>
                    <Popconfirm title="确定删除此编号规则?" okText="是" cancelText="否"
                                onConfirm={() => this.handleDelete(record.id)}>
                        <Icon type="delete"/>
                    </Popconfirm>
                </span>,
            }, {
                title: "",
                dataIndex: ""
            }
        ];
        const {
            dataSource,
            loading
        } = this.props;

        return (
            <Table
                {...tableProps}
                columns={columns}
                scroll={{x: 1500, y: 'calc(100vh - 480px)'}}
                dataSource={dataSource}
                loading={loading}
            />
        );
    }
}

const mapStateToProps = state => {
    return {
        ...state.businessCode.list
    };
};

export default connect(mapStateToProps)(List);
